home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Arsenal Files 4
/
The Arsenal Files 4 (Arsenal Computer).ISO
/
casm
/
au116-as.exe
/
FIXCR.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1994-11-18
|
4KB
|
185 lines
// FIXCR.CPP 1 1 6666
// Dave Harris 11 11 6
// Compiled using Borland C++ ver 3.1 1 1 1 1 6666
// 03-03-94 1 .. 1 6 6
// 11111 .. 11111 666
////////////////////////////////////////////////////////////////////////
#include "au.hpp"
#include <time.h>
#define PROGRAM "FIXCR" // Name of module
/*********************************************************************/
#define BUF_SIZE 256
#define TMP_FILE "FIXCR.TMP"
typedef struct
{
char tmp_file[FLENGTH];
} FIXCR_INFO;
/*░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░*/
static void buffered_write(HANDLE *handle, char ch, char *buffer, int *pos)
{
if (ch == -1) /* Write the string even if not full */
{
if (handle->write_raw(buffer, *pos) != *pos)
{
fprintf(stderr, "\aWrite Error %d\n", errno);
exit(1);
}
*pos = 0;
}
else
{
buffer[*pos] = ch;
(*pos)++;
if (*pos == BUF_SIZE)
{
if (handle->write_raw(buffer, *pos) < 0)
{
fprintf(stderr, "\aWrite Error %d\n", errno);
exit(1);
}
*pos = 0;
}
}
}
/*░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░*/
static int fixcr(AU *au, char *file_name)
{
HANDLE in_handle, out_handle;
int ch, ch2;
int opt;
int modified = FALSE;
int binary_ok = FALSE; /* File is binary, but user answered yes */
char buffer[BUF_SIZE];
int pos = 0;
FIXCR_INFO *in = (FIXCR_INFO *)au->info;
if (!au->no_extra)
printf("@?6Processing @?1%s@?H\n", file_name);
if (in_handle.open(au, file_name, O_RDONLY | O_BINARY) != SUCCESS)
return -1;
if (out_handle.open(au, in->tmp_file, O_RDWR | O_BINARY | O_CREAT) != SUCCESS)
{
in_handle.close();
return -1;
}
ch = in_handle.read_char();
for (;;)
{
if (ch == EOF)
break;
if (is_binary_file_char(ch) && !binary_ok)
{
fprintf(stderr, "File %s appears to be a binary file. Still fix it Y/N:",
file_name);
opt = toupper(my_getch());
fprintf(stderr, "\n");
if (opt != 'Y')
{
modified = FALSE;
break;
}
else
binary_ok = TRUE;
}
ch2 = in_handle.read_char();
if (ch == '\r' && ch2 != '\n' || ch == '\n')
{
buffered_write(&out_handle, '\r', buffer, &pos);
buffered_write(&out_handle, '\n', buffer, &pos);
ch = ch2;
modified = TRUE;
}
else if (ch == '\r' && ch2 == '\n')
{
buffered_write(&out_handle, '\r', buffer, &pos);
buffered_write(&out_handle, '\n', buffer, &pos);
ch = in_handle.read_char();
}
else
{
buffered_write(&out_handle, ch, buffer, &pos);
ch = ch2;
}
}
buffered_write(&out_handle, -1, buffer, &pos);
out_handle.close();
in_handle.close();
if (modified)
{
unlink(file_name);
rename(in->tmp_file, file_name);
au->number_changed++;
}
else
unlink(in->tmp_file);
au->number_processed++;
return 0;
}
/*░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░*/
static BYTE parse_comm_line(AU *au, char option, char *, PARSE_TYPE type)
{
switch (type)
{
case PARSE_PARAM_OPTION:
switch (option)
{
case '?':
au_standard_opt_header(au, "FIXcr", NULL);
exit(0);
default:
au_invalid_option(au, PROGRAM, option);
}
return TRUE;
}
return FALSE;
}
/*░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░*/
static void end_program(void)
{
FIXCR_INFO *in = (FIXCR_INFO *)glob_au->info;
unlink(in->tmp_file);
return;
}
/*░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░*/
int main_fixcr(AU *au, int argc, char *argv[])
{
FIXCR_INFO *in;
in = new FIXCR_INFO;
memset(in, '\0', sizeof(FIXCR_INFO));
au->info = in;
generic_parse_comm_line(au, argc, argv, parse_comm_line);
build_fname(in->tmp_file, au->cur_directory, TMP_FILE);
atexit(end_program); /* Remove temporary if it dies */
process_files(au, fixcr);
if (!au->no_extra)
au_printf_c(au, 15, "\nFiles Processed = %d\n", au->number_processed);
if (au->number_processed > 0)
{
if (!au->no_extra)
au_printf_c(au, 15, "Files Modified = %d\n", au->number_changed);
}
return 0;
}